package com.register;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.dao.SearchUsers;
import com.util.HbnUtil;

import entities.*;
/**
 * Servlet implementation class Register
 */
public class Register extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Register() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		Users user = new Users();
		response.setContentType("text/html");
		Session se = HbnUtil.getSession();
		Transaction tran = se.beginTransaction();
		user.setEmail(request.getParameter("Email"));
		user.setName(request.getParameter("GivenName") + request.getParameter("Surname"));
		//user.setDatejoint(datejoint);
		user.setPassword(request.getParameter("Password"));
		user.setPhone(request.getParameter("Phone"));
		se.save(user);
		tran.commit();
		response.sendRedirect("try.jsp");
/*		
		response.setContentType("text/html");
		String email = request.getParameter("Email");
		String password = request.getParameter("Password");
		HttpSession session = request.getSession(true);	
		
		Users u = SearchUsers.searchUser(email, password);
		if(u!= null)
		{
			
			session.setAttribute("Email", u.getEmail());
			session.setAttribute("Name", u.getName());
			session.setAttribute("Userid", u.getUserid());
			session.setAttribute("Password", u.getPassword());
			request.setAttribute("show", "useraccount.jsp");
			request.getRequestDispatcher("try.jsp").forward(request, response);
		}
		response.sendRedirect("index.jsp");*/
		
	}

}
